/*
 * gc_test_manyObjects.c
 *
 *  Created on: Dec 9, 2011
 *      Author: Rui Zhuang
 */
#include "gc.h"

struct A {
	struct A* a;
};

int main(int argc, char** argv){
	int userSize = 0;
	if(argc == 2){
		char* size = argv[1];
		userSize = atoi(size);
	}
	if(userSize > 0){
		printf("User provided size : %d\n",userSize);
		gc_init_max_heap_size(userSize);
	}

	struct A* a;
	gc_add_root_node(&a);

	printf("Sizeof struct A is %d\n",sizeof(struct A));
	int i = 1000000;
	printf("Maximum memory size avaiable = %d\n", g_nMaxHeapSize);
	printf("Before objects allocation: Current allocated heap size = %d\n", g_nCurrentHeapSize);
	a = (struct A*)gc_malloc(sizeof(struct A));
	struct A* temp = a;
	gc_add_root_node(&temp);
	while(i > 1){
		temp->a = (struct A*)gc_malloc(sizeof(struct A));
		gc_free_root_node(&temp);
		temp = temp->a;
		i=i-1;
	}
	printf("After 1 million objects allocated: Current allocated heap size = %d\n", g_nCurrentHeapSize);

	gc_free_root_node(&a);
	gc_free_root_node(&temp);
	gc_recycle();
	printf("After recycle: current allocated heap size = %d\n", g_nCurrentHeapSize);
	printf("Successfully run gc_test_manyObjects\n");
	return 0;
}
